<template>
	<view class="search">
		<!-- 顶部 搜索样式 -->
		<view class="nav-search">
			<view class="nav-search-left">
				<view class="nav-search-back" @click="back">
					<view class="back-icon">
						<u-icon name="arrow-left" size="30"></u-icon>
					</view>
					<view class="back-text">
						返回
					</view>
				</view>
			</view>
			<view class="nav-search-right">
				<view class="ipt-box" :style="isFlag?'':'border-radius:50rpx'">
					<uni-icons type="search" color="#0c34ba" size="18"></uni-icons>
					<input type="search" confirm-type="search" @confirm="doSearch" class="ipt" v-model="text" />
				</view>
				<view class="cancel" v-show="isFlag" @click="del">
					取消
				</view>
			</view>
		</view>
		<!-- 顶部 搜索样式 -->
		<!-- 背景 -->
		<view class="bg">

		</view>
		<!-- 背景 -->
		<!-- 商品结构 -->
		<view class="product">
			<view class="product-box" v-for="(item,index) in searchList" :key="index">
				<view class="img-box">
					<image :src="item.smallImg" mode="widthFix center" class="img"></image>
				</view>
				<view class="product-name product-common-name">
					{{item.name}}
				</view>
				<view class="product-enname product-common-name">
					{{item.enname}}
				</view>
				<view class="product-price">
					{{item.price}}
				</view>
			</view>
			<view style="text-align:center;width:100%;" v-show="searchList == ''">
				<u-empty text="没有搜索结果" mode="search"></u-empty>
			</view>



		</view>

		<!-- 商品结构 -->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				text: "",
				isFlag: true,
				searchList: ""

			}
		},
		watch: {
			text() {
				this.change()
			}
		},
		onLoad(option) {
			this.text = option.text
			this.change()
			this.doSearch()
		},
		methods: {
			// 搜索
			doSearch() {
				this.getSearch()
			},
			getSearch() {
				uni.showLoading({
					title: "正在加载...",
					mask: true
				})
				uni.request({
					url: "http://www.kangliuyong.com:10002/search",
					data: {
						appkey: "U2FsdGVkX19WSQ59Cg+Fj9jNZPxRC5y0xB1iV06BeNA=",
						name: this.text
					},
					success: (res) => {

						if (res.statusCode === 200) {
							this.searchList = res.data.result
							if (this.searchList == '') {
								uni.showToast({
									title: "未查询到数据",
									icon: "none",
									mask: true
								})
							}
							uni.hideLoading()
						}
					}

				})
			},
			change() {
				// 判断 输入框是否 为空
				if (this.text == "") {
					this.isFlag = false
				} else {
					this.isFlag = true
				}
			},
			del() {
				// 清空 输入框
				this.text = ""
			},
			back() {
				//返回上一级 这个不合理，多个搜索未能返回上一个页面，需更改
				uni.navigateBack({
					delta: 1
				})
			}

		},
	}
</script>
<style lang="less" scoped>
	.search {
		height: 100vh;
		background-color: #f2f2f2;

		.nav-search {
			height: 100rpx;
			font-size: 34rpx;
			background-color: #fff;
			display: flex;
			align-items: center;

			.nav-search-left {
				height: 70rpx;
				padding: 0 30rpx;
				flex: 2;

				.nav-search-back {
					color: #0c34ba;
					display: flex;
					justify-content: center;
					align-items: center;

					.back-icon {
						margin-right: 20rpx;
					}

					.back-text {
						height: 70rpx;
						line-height: 70rpx;
					}
				}
			}

			.nav-search-right {
				height: 70rpx;
				flex: 8;
				display: flex;

				.ipt-box {
					padding: 0 30rpx;
					background-color: #f7f8fa;
					border-radius: 60rpx 0 0 60rpx;
					display: flex;
					align-items: center;

					.ipt {}
				}

				.cancel {
					width: 120rpx;
					padding-right: 20rpx;
					text-align: center;
					line-height: 70rpx;
				}
			}
		}

		.bg {
			height: 300rpx;
			background-color: #0c34ba;
		}

		.product {
			position: relative;
			top: -50rpx;
			border-radius: 30rpx 30rpx 0 0;
			padding: 30rpx 0;
			margin: 0 20rpx;
			background-color: #fff;
			display: flex;
			justify-content: flex-start;
			flex-wrap: wrap;

			.product-box {
				width: 200rpx;
				margin-left: 30rpx;
				margin-bottom: 30rpx;
				background-color: #fff;

				.img-box {
					width: 100%;
					height: 200rpx;

					.img {
						width: 100%;
						height: 100%;
						display: block;
					}
				}

				.product-common-name {
					height: 50rpx;
					text-align: start;
					display: -webkit-box;
					white-space: pre-wrap;
					overflow: hidden;
					text-overflow: ellipsis;
					-webkit-box-orient: vertical;
					-webkit-line-clamp: 1;
				}

				.product-name {
					color: #646566;
					font-size: 36rpx;
					margin-bottom: 10rpx;
					margin-top: 20rpx;

				}

				.product-enname {
					color: #b3b5b3;
					font-size: 32rpx;
					margin-bottom: 10rpx;
				}

				.product-price {
					color: #0c34ba;
					font-size: 32rpx;
					font-weight: bold;
					padding-bottom: 20rpx;
				}
			}
		}
	}
</style>
